package com.topica;

import java.io.File;

import android.app.Activity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.topica.util.Util;

public class AudioActivity extends Activity {

	public static final String PATH = "com.topica.AudioActivity.PATH";
	private static final String TAG = "Audio Activity";
	private TextView currentSec;
	private TextView duration;
	private SeekBar seekBar;
	private ImageButton btnPlayPause;
	private TextView audioName;
	private Handler handler;
	private MediaPlayer mediaPlayer;
	private String extra;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		setContentView(R.layout.activity_audio);
		findViews();
		handler = new Handler();
		init();
	}

	private void init() {

		btnPlayPause.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (btnPlayPause.getContentDescription().equals(
						getResources().getString(R.string.play))) {
					btnPlayPause.setContentDescription(getResources()
							.getString(R.string.pause));
					btnPlayPause.setBackgroundResource(R.drawable.pause);
					try {
						mediaPlayer.start();
						updateProgress();
					} catch (IllegalStateException e) {
						mediaPlayer.pause();
					}
				} else {
					btnPlayPause.setContentDescription(getResources()
							.getString(R.string.play));
					btnPlayPause.setBackgroundResource(R.drawable.play);
					mediaPlayer.pause();
				}
			}
		});
		extra = getIntent().getStringExtra(PATH);
		if (null == extra || extra.equals("")) {
			Toast.makeText(AudioActivity.this, R.string.no_mp3,
					Toast.LENGTH_SHORT).show();
			finish();
			return;
		}
		if (!extra.contains("mp3")) {
			Toast.makeText(AudioActivity.this, R.string.no_mp3,
					Toast.LENGTH_SHORT).show();
			finish();
			return;
		}
		String audioPath = Environment.getExternalStorageDirectory()
				+ "/topica/" + extra;
		File file = new File(audioPath);
		if (!file.exists()) {
			Toast.makeText(AudioActivity.this, R.string.no_mp3,
					Toast.LENGTH_SHORT).show();
			finish();
			return;
		}
		Log.i(TAG, "Path audio = " + audioPath);
		mediaPlayer = MediaPlayer.create(this, Uri.parse(audioPath));
		seekBar.setMax(mediaPlayer.getDuration());
		audioName.setText(Util.getFileName(audioPath));
		seekBar.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				seekChange(arg0);
				return false;
			}
		});
		duration.setText(Util.displayAudioTime(mediaPlayer.getDuration()));
		currentSec.setText("00:00");
	}

	private void findViews() {
		currentSec = (TextView) findViewById(R.id.txt_current_sec);
		duration = (TextView) findViewById(R.id.txt_duration);
		seekBar = (SeekBar) findViewById(R.id.seekbar);
		btnPlayPause = (ImageButton) findViewById(R.id.btn_play_pause);
		audioName = (TextView) findViewById(R.id.txt_audio_name);
	}

	public void updateProgress() {

		seekBar.setProgress(mediaPlayer.getCurrentPosition());
		currentSec.setText(Util.displayAudioTime(mediaPlayer
				.getCurrentPosition()));
		if (mediaPlayer.isPlaying()) {
			Runnable seek = new Runnable() {
				public void run() {
					updateProgress();
				}
			};
			handler.postDelayed(seek, 1000);
		} else {
			btnPlayPause.setContentDescription(getResources().getString(
					R.string.play));
			btnPlayPause.setBackgroundResource(R.drawable.play);
			mediaPlayer.pause();
		}
	}

	private void seekChange(View v) {
		if (mediaPlayer.isPlaying()) {
			SeekBar sb = (SeekBar) v;
			mediaPlayer.seekTo(sb.getProgress());
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		if (mediaPlayer.isPlaying()) {
			mediaPlayer.pause();
		}
	}

	@Override
	protected void onStop() {
		super.onStop();
		if (mediaPlayer != null) {
			if (mediaPlayer.isPlaying()) {
				mediaPlayer.stop();
				mediaPlayer.release();
			}
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (mediaPlayer != null) {
			if (mediaPlayer.isPlaying()) {
				mediaPlayer.stop();
				mediaPlayer.release();
			}
		}
	}

}
